﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.RKWeigh;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.RKWeighData_
{
    /// <summary>
    /// 添加分割称重入库
    /// </summary>
    public class AddRKWeighDataHandler : IRequestHandler<AddRKWeighDataCommand, Result>
    {
        public AddRKWeighDataHandler(IBaseRepository<RKWeighData> baseRepository, IMapper mapper)
        {
            this.RKWeighDataRepository = baseRepository;
            this.mapper = mapper;
        }

        public IBaseRepository<RKWeighData> RKWeighDataRepository { get; set; }
        public IMapper mapper { get; set; }
        public async Task<Result> Handle(AddRKWeighDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            var data=mapper.Map<RKWeighData>(request);
            var AddNum= RKWeighDataRepository.Add(data);
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
